﻿
@{
    ViewData["Title"] = "文章列表";
    Layout = "~/Views/Shared/_Back.cshtml";
}
@model IEnumerable<IVBlog.Models.Article>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <h2>所有文章</h2>
                    <br />
                </div>
                <div class="layui-card-body layui-table-body layui-table-main">
                    <table class="layui-table layui-form">
                        <thead>
                            <tr>
                                @*<th>
                                        <input type="checkbox" lay-filter="checkall" name="" lay-skin="primary">
                                    </th>*@
                                <th>文章编号</th>
                                <th>文章标题</th>
                                <th>发表日期</th>
                                <th>修改日期</th>
                                <th>文章类别</th>
                                <th>文章状态</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var item in Model)
                            {
                                <tr>
                                    @*<td>
                                        <input type="checkbox" lay-filter="checkall" name="" lay-skin="primary">
                                    </td>*@
                                    <td>@item.ArticleId</td>
                                    <td>@item.Aname</td>
                                    <td>@item.Atime</td>
                                    <td>@(item.AreTime==null?"无修改":""+item.AreTime)</td>
                                    <td>@item.Category.Cname</td>
                                    <td><span style="color:@(item.Astate == 0 ? "red" : "forestgreen")">@(item.Astate == 0 ? "未发布" : "已发布")</span></td>
                                    <td class="td-manage">
                                        <div class="layui-btn-group">
                                            <a href="~/Back/EditArticle/@item.ArticleId" class="layui-btn layui-btn-sm"><i class="layui-icon">&#xe642;</i>编辑</a>
                                            <button type="button" class="layui-btn layui-btn-sm @(item.Astate==0?"":"layui-disabled")" onclick="Publish(@item.ArticleId)" @(item.Astate==0?"":"disabled")><i class="layui-icon">&#xe654;</i>发布</button>
                                            <button type="button" class="layui-btn layui-btn-danger layui-btn-sm" onclick="Del(@item.ArticleId)"><i class="layui-icon">&#xe640;</i>删除</button>
                                        </div>
                                    </td>
                                </tr>
                            }
                        </tbody>
                    </table>
                </div>
                <div class="layui-card-body">
                    <div class="page text-center">
                        <div id="pagecard">
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@section Scripts{
    <script>
        layui.use('laypage', function () {
            var laypage = layui.laypage;
            var totalRows = @ViewBag.totalRows;
            var totalPage = @ViewBag.totalPage;
            var pageIndex = @ViewBag.pageIndex;
            var pageSize = @ViewBag.pageSize;
            //执行一个laypage实例
            laypage.render({
                elem: 'pagecard' //注意，这里的 test1 是 ID，不用加 # 号
                , count: totalRows //数据总数，从服务端得到
                , limit: pageSize //每页显示的条数
                , curr: pageIndex
                , jump: function (obj, first) {
                    //obj包含了当前分页的所有参数，比如：
                    console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
                    console.log(obj.limit); //得到每页显示的条数
                    //首次不执行
                    if (!first) {
                        if (pageIndex != obj.curr) {
                            location.href = "../Back/ArticleList?pageIndex=" + obj.curr + "&pageSize=" + obj.limit;
                        }
                    }
                }
            });
        });
        function Publish(DremarkId) {
            var layer = layui.layer;
            layer.confirm('确定发布吗？', {
                btn: ['确定', '取消'] //按钮
            }, function () {
                $.ajax({
                    type: 'post',
                    url: '../Back/Publish/' + DremarkId,
                    success: function () {
                        layer.msg("修改状态成功", { icon: 1 });
                        setTimeout(function () {
                            window.location.reload();
                        }, 2000);
                    },
                    error: function () {
                        layer.msg("修改失败", { icon: 2 });
                    }
                });
            }, function () {
                layer.msg('取消了发布', {
                    time: 10000, //10s后自动关闭
                    btn: ['明白了']
                });
            });
        };
        function Del(DremarkId) {
            var layer = layui.layer;
            layer.confirm('确定删除吗？', {
                btn: ['确定', '取消'] //按钮
            }, function () {
                $.ajax({
                    type: 'post',
                    url: '../Back/DelArticle/' + DremarkId,
                    success: function () {
                        layer.msg("文章删除成功", { icon: 1 });
                        setTimeout(function () {
                            window.location.reload();
                        }, 2000);
                    },
                    error: function () {
                        layer.msg("文章删除失败", { icon: 2 });
                    }
                });
            }, function () {
                layer.msg('取消了删除', {
                    time: 10000, //10s后自动关闭
                    btn: ['明白了']
                });
            });
        };
    </script>
}

